More Ruby Tidbits

The fun (if frustrating) part of a job in computing is you’re always picking up new information. Sometimes it’s transformational, other times they just help make your daily work a bit easier.

 

Here are some in the latter category that I’ve recently discovered about Ruby.

 

Open URI and redirects

If you use open-uri to read documents, you might occasionally run across an issue with HTTPS redirects, where the read will fail. There are some includes that will solve this problem in the background, but to reduce dependencies, you can use open-uri’s HTTPRedirect and error handling to resolve the problem in just a few tries: http://stackoverflow.com/questions/27407938/ruby-open-uri-redirect-forbidden

 

Dynamic methods

A good programmer is always looking for ways to make code more dynamic and less reliant on manual coding. For instance, you might want to indicate an operator or method as a parameter, but with that coming in as a string, it can be hard to make good use of. To help you with this kind of issue, Ruby has a nifty “send” method which will give you the results you need: http://stackoverflow.com/questions/19733737/how-do-i-convert-a-math-operator-character-to-perform-its-operation

 

Find the call stack with pry

If you use pry to debug your Ruby code, you might get frustrated trying to sort out the stack status at the moment of the call, and how you got where you are at the moment the binding hit. There are a bunch of gems that can help you there: http://stackoverflow.com/questions/15303103/pry-show-me-the-stack

 

Happy building!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *